<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/public/header.jsp"%>
<%
    String callback = request.getParameter("callback");
	String isShow = request.getParameter("isShow") == null ? "" : request.getParameter("isShow");
	String xm_id = request.getParameter("xm_id") == null ? "" : request.getParameter("xm_id");
	String xm_code = request.getParameter("xm_code") == null ? "" : request.getParameter("xm_code");
	String xm_name = request.getParameter("xm_name") == null ? "" : request.getParameter("xm_name");
	Map<String, Object> sessionMap = (Map<String, Object>) session.getAttribute("LOGIN_USER");
	String person_id = String.valueOf(sessionMap.get("id"));
	String person = String.valueOf(sessionMap.get("truename"));
	String id= request.getParameter("id") == null ? "" : request.getParameter("id");
	boolean isAdd = "".equals(id);
    int flag = 0;
    if(!isAdd){
    	flag = 1;
    }
%>
<html>
	<head>
		<title><%=isAdd?"结算项目收款":"编辑结算收款" %></title>
		<script type="text/javascript" src="<%=contextPath %>/public/js/extend.js"></script>
		<style>
			.td1{
				padding-top:10px;
				padding-bottom:5px;
				font-size: 12px;
				background-color: #c0ebd7;
				width:20%;
			}
			.td2{
				padding:5px
			}
		</style>
	<script type="text/javascript">
		var lastIndex;
	var submitUrl = '<%=contextPath %>/springMVC/xmFinance/addXMEndInMoney';
	var turl='<%=contextPath %>/springMVC/xmFinance/listXMInvoiceByXMId?xm_id=<%=xm_id%>&status=3';
		$(function(){
			$('#warranty_value').numberbox('disable',true);
			$('#in_money').numberbox('disable',true);
			$.ajax({
				type:'POST',
				url:'<%=contextPath %>/springMVC/xmFinance/hasXMEndInMoney?type=1&xm_id=<%=xm_id%>',
				async:false,
				success:function(rtjson){
					if(rtjson != '0'){
						submitUrl = '<%=contextPath %>/springMVC/xmFinance/updateXMEndInMoney';
						turl='<%=contextPath %>/springMVC/xmFinance/listXMEndInMoneyDetail?xm_id=<%=xm_id%>&status=3';
					}
				}
			});

			  $('#ff').form('load','<%=contextPath %>/springMVC/xm/getEndInMoneyXMById?xm_id=<%=xm_id%>');
			  $('#table').datagrid({
	         	url:turl,
				rownumbers:true,
				fitColumns:true,
				nowrap:false,
				frozenColumns:[[
					{field:'ck',checkbox:true},
				    {title:'开票id',field:'id',hidden:true}
				]],
				columns:[[
					{title:'开票id2',field:'xm_invoice_id',hidden:true},
					{title:'票号',field:'invoice_code',width:200,align:'center',formatter:function(value,rowData,index){
						if(isNaN(value)){
							return "无";
						}
						return value;
					}},
					{title:'发票抬头',field:'invoice_title',width:350,align:'center'},
					{title:'开票额(元)',field:'account_receivable',width:200,align:'center'},
					{title:'进度收款金额(元)',field:'has_receivable',width:200,align:'center'},
					{title:'结算尾款金额(元)',field:'end_pay',width:200,align:'center',editor:{
						type:'numberbox',
						options:{
							precision:2,
							myFlag:'1'
						}
					},formatter:function(value,rowData,index){
					if(isNaN(value)){
							rowData.end_pay=rowData.account_receivable-rowData.has_receivable;
					}
					return rowData.end_pay;
					}},
					{title:'备注',field:'remark',width:200,align:'center',editor:{
						type:'textarea'
					}}
				]],
				onBeforeLoad:function(){
					$(this).datagrid('rejectChanges');
				},
				onLoadSuccess:function(){
				
				},
				onClickCell:function(rowIndex,field,value){
					if (lastIndex != rowIndex){
						$('#table').datagrid('endEdit', lastIndex);
						$('#table').datagrid('beginEdit', rowIndex);
						var ed = $('#table').datagrid('getEditor', {index:rowIndex,field:field});
						if(ed == null){
							ed = $('#table').datagrid('getEditor', {index:rowIndex,field:'end_pay'});
						}
						$(ed.target).select();
					}
					lastIndex = rowIndex;
				},
				onLoadSuccess:function(rtJson){
					if(rtJson.rtState== '1')
					{	
						$.messager.alert('提示',rtJson.rtMsrg);
						calculaMoney();
					}
				},
				toolbar:[
					{
					id:'save',
					text:'保存修改',
					disabled:false,
					iconCls:'icon-save',
					handler:function(){
						$('#table').datagrid('acceptChanges');
						lastIndex =-1;
						calculaMoney();	
					}
					}
			   ]
			});
			
		});
		function calculaMoney(){	
				var inMoneys=0;
				var jsondata=$("#table").datagrid("getData");
				for(var i=0;i<jsondata.total;i++){
				inMoneys+=parseFloat(jsondata.rows[i].end_pay);	
				}
				$('#in_money').numberbox('setValue', inMoneys);
				var out_invoice= parseFloat(jQuery("#out_invoice").val());
				var in_money= parseFloat(jQuery("#in_money").val());
				var has_receivable= parseFloat(jQuery("#has_receivable").val());
			    $('#warranty_value').numberbox('setValue', (out_invoice-in_money-has_receivable));
				
				var warranty_value= parseFloat(jQuery("#warranty_value").val());
				if((out_invoice-in_money-has_receivable)<warranty_value){
				$.messager.alert('提示','结算收款金额过高，请修正!');
				return 0;
				}else if((out_invoice-in_money-has_receivable)>warranty_value){
				$.messager.alert('提示','结算收款金额过低，请修正!');
				return 0;
				}
			return 1;	
				
		}
		function addXMInvoiceRequest(obj){
			$('#table').datagrid('acceptChanges');
			lastIndex =-1;
			
			var calR=calculaMoney();
			if(calR==0){
			return;
			}
			//自动提交数据
			var xm_id =jQuery("#xm_id").val();	
			var xm_name=encodeURIComponent(jQuery("#xm_name").val());	
			var xm_code=jQuery("#xm_code").val();
			var end_inmoney_remark=encodeURIComponent(jQuery('#end_inmoney_remark').val());
			if(!checkNullOREmpty(xm_id,'项目为必填项')){return;}
			var in_money=jQuery("#in_money").val();
			var other_account_receivable=jQuery("#other_account_receivable").val();
			var manage_money=jQuery("#manage_money").val();
			var warranty_value=jQuery("#warranty_value").val();
			var alarm_time=jQuery("#alarm_time").datebox("getValue");
		    if((parseFloat(in_money)+parseFloat(other_account_receivable))<parseFloat(manage_money)){
		    	$.messager.alert('提示','收款金额不能小于管理费！');
				return ;
		    }
		    var inMoneys="";
				var jsondata=$("#table").datagrid("getData");
				for(var i=0;i<jsondata.total;i++){
					if(inMoneys!=""){
						inMoneys+=";";
					}
					inMoneys+=jsonToStr(jsondata.rows[i]);	
				}
			var param = "warranty_value="+warranty_value+"&alarm_time="+alarm_time+"&end_inmoney_remark="+end_inmoney_remark
			+"&in_money="+in_money+"&manage_money="+manage_money+"&other_account_receivable="+other_account_receivable
			+"&xm_id="+xm_id+"&xm_code="+xm_code+"&xm_name="+xm_name+"&inMoneys="+inMoneys;
			$(obj).hide();
			$.ajax({
				type:'POST',
				url:submitUrl,
				data:param,
				dataType:'json',
				success:function(rtJson){
					if(rtJson.rtState == '0'){
						eval('window.opener.<%=callback%>();');
						window.close();
					}
					else{
						$.messager.alert("提示",rtJson.rtMsrg);
					}
					$(obj).show();
				}
			});
		}

	</script>
	</head>
	<body>
		<div style="width: 97%;padding-left:10px">
		<form id="ff">
			<table  style="border-collapse: collapse;" width="100%" border="1px" bordercolor="#0eb83a">
				<tr>
					<td align="right" class="td1" nowrap>项目编号：</td>
					<td class="td2" nowrap>
						<input  id="xm_code" name="xm_code" readOnly value="<%=xm_code%>">
						<input type="hidden" id="xm_id" name="xm_id" readOnly value="<%=xm_id%>">
						
					</td>
					<td align="right" class="td1" nowrap>项目名称：</td>
					<td class="td2" nowrap>
						<input  id="xm_name" name="xm_name" readOnly value="<%=xm_name%>">
					</td>
				</tr>
				<tr>	
					<td align="right" class="td1" nowrap>已开票金额：</td>
					<td class="td2" nowrap>
						<input  id="out_invoice" name="out_invoice" readOnly value=0>
					</td>
					<td align="right" class="td1" nowrap>进度收款金额：</td>
					<td class="td2">
						<input  id="has_receivable" name="has_receivable" readOnly value=0>
					</td>
				</tr>
				<tr>
			        <td align="right" class="td1" nowrap>结算尾款总额：</td>
					<td class="td2" >
					<input  id="in_money" name="in_money"  class="easyui-numberbox" data-options="min:0,precision:2" value="0" >
					</td>
					<td align="right" class="td1" nowrap>新增管理费用：</td>
					<td class="td2"  >
						<input  id="manage_money" name="manage_money"  class="easyui-numberbox" data-options="min:0,precision:2" value="0">	
						<input type="hidden" id="other_account_receivable" name="other_account_receivable"    value="0">	
					</td>
				</tr>
				<tr>
			        <td align="right" class="td1" nowrap>质保金：</td>
					<td class="td2" >
					<input  id="warranty_value" name="warranty_value"  class="easyui-numberbox" data-options="min:0,precision:2" value="0">
					</td>
					<td align="right" class="td1" nowrap>预警日期：</td>
					<td class="td2" nowrap >
					<input id="alarm_time" name="alarm_time" class="easyui-datebox"/>
					</td>
				</tr>
				<tr>
					<td align="right" class="td1" nowrap>摘要：</td>
					<td class="td2" nowrap colspan='3'>
						<textarea  id="end_inmoney_remark" name="end_inmoney_remark" rows="6" style="width: 100%"></textarea>
					</td>
				</tr>
				<tr>
					<td align="right" class="td1" nowrap></td>
					<td class="td2" nowrap colspan='3'>
						<div align="left">
						   <%if(!"1".equals(isShow)){ %>
							<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save',plain:false" onclick="addXMInvoiceRequest(this);">提交</a>
							<%} %>
							<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-cancel',plain:false" onclick="javascript:window.close();">关闭</a>
						</div>
					</td>
				</tr>
				<tr>
					<td colspan="4"  nowrap>
						<div id="table"></div>
					</td>
				</tr>
			</table>
			</form>
		</div>
		
	</body>
</html>